﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using System.Data.Services.Client;
using TGIApp.Model;

namespace TGIApp.Web.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/

        public ActionResult Index()
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var roles = context.Roles;
            //return View(roles);
            return View();
        }

        //
        // GET: /Role/Details/5

        public ActionResult Details(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var role = context.Roles.Where(r => r.Id == id).FirstOrDefault();
            //return View(role);
            return View();
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(Role role)
        {
            try
            {
                // TODO: Add insert logic here
                //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                //context.AddToRoles(role);
                //context.SaveChanges();
                //return RedirectToAction("Index");
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Role/Edit/5
 
        public ActionResult Edit(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var role = context.Roles.Where(r => r.Id == id).FirstOrDefault();
            //return View(role);
            return View();
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Role updatedRole)
        {
           
               // // TODO: Add update logic here
               // var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
               // context.MergeOption= MergeOption.OverwriteChanges;
                
               // //var oldRole = context.Roles.Where(r => r.Id == id).FirstOrDefault();
               ////oldRole.Name = updatedRole.Name;
               // context.AttachTo("Roles", updatedRole);
               // context.UpdateObject(updatedRole);
               // context.SaveChanges();
               // return RedirectToAction("Index");
            return View();
          
        }

        //
        // GET: /Role/Delete/5
 
        public ActionResult Delete(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //context.MergeOption = MergeOption.OverwriteChanges;
            //var role = context.Roles.Where(r => r.Id == id).FirstOrDefault() ;
            
            //return View(role);
            return View();
        }

        //
        // POST: /Role/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Role roleToBeDeleted)
        {
           
                //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                ////var role = context.Roles.Where(r => r.Id == id).FirstOrDefault();
                //Role _role = new Role() { Id = id };
                //context.AttachTo("Roles", _role);
                //context.DeleteObject(_role);
                //context.SaveChanges();
                //return RedirectToAction("Index");
            return View();
        }
    }
}
